package cn.gzmli.dtt.common.workflow.service;

import cn.gzmli.dtt.common.entity.PageInfo;
import cn.gzmli.dtt.common.utils.BaseService;
import cn.gzmli.dtt.common.utils.BaseUtil;
import cn.gzmli.dtt.common.utils.Constant;
import com.xiaoleilu.hutool.util.StrUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import javax.transaction.SystemException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Descript
 * @Author xsx
 * @Date 2018/1/23
 */
@Service
@Transactional
public class WorkProgressService extends BaseService {
    // 业务类型对应流程记录
    public enum BType {

        TYPE1("1", "workflowlog", Constant.LogType._1_TotalPlanFlowLog);


        private String workFlowName;
        private String tableName;
        private Integer type;

        BType(String workFlowName, String tableName, Integer type) {
            this.workFlowName = workFlowName;
            this.tableName = tableName;
            this.type = type;
        }

        public String getWorkFlowName(){
            return this.workFlowName;
        }

        public String getTableName(){
            return this.tableName;
        }

        public Integer getType(){
            return this.type;
        }
    }

    public List<Map<String, Object>> getWorkProgress(String workFlowID, Integer bID, Integer order, PageInfo pageInfo) throws Exception {
        if (StrUtil.isBlank(workFlowID)){
            throw new SystemException("参数 workFlowID 不能为空");
        }
        if (bID==null){
            throw new SystemException("参数 bID 不能为空");
        }
        StringBuilder sql = new StringBuilder();
        sql.append("SELECT * FROM ")
            .append("(SELECT ")
            .append("t1.id AS tID,")
            .append("t1.flowNodeID,")
            .append("fn1.name AS flowNodeName,")
            .append("t1.status AS tStatus,")
            .append("t1.forwarderID,")
            .append("forwarder1.name AS forwarderName,")
            .append("t1.processerID,")
            .append("processer1.name AS processerName,")
            .append("t1.createTime AS reviceTime,")
            .append("t1.procTime ")
            .append("FROM tasklog t1 ")
            .append("LEFT JOIN flownode fn1 ON fn1.id=t1.flowNodeID ")
            .append("LEFT JOIN user forwarder1 ON forwarder1.id=t1.forwarderID ")
            .append("LEFT JOIN user processer1 ON processer1.id=t1.processerID ")
            .append("WHERE fn1.workFlowID=? AND t1.formID=? ")
            .append("UNION ALL ")
            .append("SELECT ")
            .append("ht.tID,")
            .append("ht.flowNodeID,")
            .append("fn2.name AS flowNodeName,")
            .append("ht.status AS tStatus,")
            .append("ht.forwarderID,")
            .append("forwarder2.name AS forwarderName,")
            .append("ht.processerID,")
            .append("processer2.name AS processerName,")
            .append("ht.taskLogCreateTime AS reviceTime,")
            .append("ht.procTime ")
            .append("FROM historytasklog ht ")
            .append("LEFT JOIN tasklog t2 ON t2.id=ht.tID ")
            .append("LEFT JOIN flownode fn2 ON fn2.id=ht.flowNodeID ")
            .append("LEFT JOIN user forwarder2 ON forwarder2.id=ht.forwarderID ")
            .append("LEFT JOIN user processer2 ON processer2.id=ht.processerID ")
            .append("WHERE fn2.workFlowID=? AND ht.bID=?)tab ");
        if (order!=null&&order==0){
            sql.append(" ORDER BY tID ASC");
        }else {
            sql.append(" ORDER BY tID DESC");
        }
        return BaseUtil.getData(sql, pageInfo, this.getDao(), workFlowID, bID, workFlowID, bID);
    }

    public List<Map<String, Object>> getWorkFlowLogs(HttpServletRequest request, PageInfo pageInfo) throws Exception {
        Map<String, Object> condition = this.getCondition(request);
        StringBuilder sql = new StringBuilder();
        sql.append("SELECT * FROM ")
            .append("(SELECT ")
            .append("this_.*,")
            .append("processer.name AS processerName , ")
            .append("processer.headerpath AS headerpath ")
            .append("FROM workflowlog").append(" this_ ")
            .append("LEFT JOIN user processer ON processer.id=this_.processerID)tab ")
            .append("WHERE 1=1 ");
        if (StrUtil.isNotBlank(request.getParameter("order"))&&"0".equals(request.getParameter("order"))){
            return BaseUtil.getData(sql, condition, pageInfo, this.getDao(), "id", true);
        }else{
            return BaseUtil.getData(sql, condition, pageInfo, this.getDao(), "id", false);
        }
    }

    private Map<String, Object> getCondition(HttpServletRequest request) throws Exception {
        Map<String, Object> condition = new HashMap<>();
        if (StrUtil.isNotBlank(request.getParameter("tID"))){
            condition.put(" AND tID=? ", Integer.valueOf(request.getParameter("tID")));
        }
        if (StrUtil.isNotBlank(request.getParameter("bID"))){
            condition.put(" AND bID=? ", Integer.valueOf(request.getParameter("bID")));
        }
        if (StrUtil.isNotBlank(request.getParameter("status"))){
            condition.put(" AND status IN("+BaseUtil.getPlaceholder(request.getParameter("status"))+") ", BaseUtil.getArgs(request.getParameter("status"), Integer.class));
        }
        if (StrUtil.isNotBlank(request.getParameter("workFlowId"))){
            condition.put(" AND workFlowId IN("+BaseUtil.getPlaceholder(request.getParameter("workFlowId"))+") ", BaseUtil.getArgs(request.getParameter("workFlowId"), Integer.class));
        }
        return condition;
    }

}
